﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class SortedGridView : System.Web.UI.UserControl
{
    private static int iNowPage = 1;
    private static int iCount;
    private DataSet datasource;
    public DataSet DataSource
    {
        get { return datasource; }
        set { datasource = value; }
    }

    private int showcount;
    public int ShowCount
    {
        get { return showcount; }
        set { showcount = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DoBind();           
        }
    }


    private void DoBind()
    {
        lbNowPage.Text = iNowPage.ToString();
        BindData(iNowPage);
        iCount = datasource.Tables[0].Rows.Count;
        lbDataCount.Text = iCount.ToString();
        if (iCount % showcount == 0)
        {
            iCount = iCount / showcount;
        }
        else
        {
            iCount = iCount / showcount + 1;
        }
        ddlGoTo.Items.Clear();
        for (int i = 1; i <= iCount; i++)
        {
            ddlGoTo.Items.Add(i.ToString());
        }
        lbCount.Text = iCount.ToString();
    }
    private void BindData(int nowpage)
    {

        DataTable dt = new DataTable();
        dt = datasource.Tables[0].Copy();
        dt.Clear();

        for (int i = showcount * (nowpage - 1); i < showcount * nowpage; i++)
        {
            DataRow row = dt.NewRow();
            try
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    row[j] = datasource.Tables[0].Rows[i][j];
                }
                dt.Rows.Add(row);
            }
            catch
            {

                break;
            }

        }
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }


    //下一页
    protected void lbNextPage_Click(object sender, EventArgs e)
    {
        if (iNowPage == iCount)
        {
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "updateScript", "alert('已经是尾页！')", true);
            return;
        }
        iNowPage++;
        lbNowPage.Text = iNowPage.ToString();
        BindData(iNowPage);
    }

    //上一页
    protected void lbLastPage_Click(object sender, EventArgs e)
    {
        if (iNowPage == 1)
        {
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "updateScript", "alert('已经是首页！')", true);
            return;
        }
        iNowPage--;
        lbNowPage.Text = iNowPage.ToString();
        BindData(iNowPage);
    }

    //首页
    protected void lbFirstPage_Click(object sender, EventArgs e)
    {
        iNowPage = 1;
        lbNowPage.Text = iNowPage.ToString();
        BindData(iNowPage);
    }

    //尾页
    protected void lbEndPage_Click(object sender, EventArgs e)
    {
        iNowPage = iCount;
        lbNowPage.Text = iNowPage.ToString();
        BindData(iNowPage);
    }

    //跳转
    protected void ddlGoTo_SelectedIndexChanged(object sender, EventArgs e)
    {
        iNowPage = int.Parse(ddlGoTo.SelectedItem.Value);
        lbNowPage.Text = iNowPage.ToString();
        BindData(iNowPage);
    }
}